package com.cn.yunhe.rentcars.web.permission;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.cn.yunhe.rentcars.mapper.permission.ResourceMapper;
import com.cn.yunhe.rentcars.mapper.permission.RoleResourceMpper;
import com.cn.yunhe.rentcars.model.permission.Resource;
import com.cn.yunhe.rentcars.model.permission.RoleResource;

@RestController
public class ResourceController {
	
	@Autowired
	private ResourceMapper resourceMapper;
	
	@Autowired
	private RoleResourceMpper roleResourceMpper;
	
	/**
	 * 展示资源信息
	 * @return 资源展示页面
	 */
	@RequestMapping(value="/resources",method=RequestMethod.GET)
	public ModelAndView showResources(){
		
		return new ModelAndView("resource-list");
	}
	
	/**
	 * 
	 * @return 跳转页面
	 */
	@RequestMapping(value="/addResource",method=RequestMethod.GET)
	public ModelAndView addResource(){
		
		return new ModelAndView("resource-add");
	}
	
	/**
	 * 根据资源id显示资源信息页面
	 * @param permisId
	 * @return Resource
	 */
	@RequestMapping(value="/showResource",method=RequestMethod.GET)
	public ModelAndView showResourceById(@RequestParam(required=false) String permisId){
		if(permisId==null || permisId.equals("")){
			permisId="0";
		}
		Resource resource = resourceMapper.getResourceById(permisId);
		Map<String,Resource> map = new HashMap<String,Resource>();
		map.put("resource",resource);
		return new ModelAndView("resource-show",map);
	}
	
	
	/**
	 * 根据资源id显示资源信息页面
	 * @param permisId
	 * @return Resource
	 */
	@RequestMapping(value="/editResource",method=RequestMethod.GET)
	public ModelAndView editResourceById(@RequestParam() String permisId){
		
		Resource resource = resourceMapper.getResourceById(permisId);
		Map<String,Resource> map = new HashMap<String,Resource>();
		map.put("resource",resource);
		return new ModelAndView("resource-edit",map);
	}
	
	
	/**
	 * 添加资源
	 * @param resource
	 */
	@RequestMapping(value="/resource/add",method=RequestMethod.POST)
	public void add(@RequestBody Resource resource){
		resource.setPermisId(UUID.randomUUID().toString());
		resourceMapper.addResource(resource);
	}
	
	/**
	 * 更新资源至数据库
	 * @param resource
	 */
	@RequestMapping(value="/resource/edit",method=RequestMethod.PUT)
	public void editSource(@RequestBody Resource resource){
		resourceMapper.updateResource(resource);
	}
	
	/**
	 * 展示ztree数据
	 * @param id
	 * @return
	 */
	@RequestMapping(value="resource/list",method=RequestMethod.POST)
	public List<Map<String,String>> getResources(@RequestParam(required=false) String id){
		
		List<Map<String,String>> nodesList = new ArrayList<Map<String,String>>();
		if(id==null || id.equals("")){
			Map<String,String> map = new HashMap<String,String>();
			map.put("id", "0");
			map.put("name", "根目录");
			map.put("isParent","true");
			nodesList.add(map);
		}else{
			List<Resource> resourceList = resourceMapper.queryByParentId(id);
			
			for(Resource e:resourceList){
				String permisId=e.getPermisId();
				String name=e.getPermisName();
				boolean isParent = false;
				if(resourceMapper.isHadSon(permisId).size()>0){
					isParent = true;
				}
				Map<String,String> map = new HashMap<String,String>();
				map.put("id", permisId);
				map.put("name",name);
				map.put("isParent",""+isParent);
				nodesList.add(map);
			}
		}
		
		return nodesList;
	}
	
	
	/**
	 * 根据也难ajax请求返回ztree数据判断是否选中
	 * @param id
	 * @param roleid
	 * @return
	 */
	@RequestMapping(value="resource/listbyid",method=RequestMethod.POST)
	public List<Map<String,String>> getResourcesByRoleid(@RequestParam(required=false) String id,@RequestParam(required=false) String roleid){
		
		List<RoleResource> rrList = roleResourceMpper.getByRoleId(Integer.parseInt(roleid));
		List<Map<String,String>> nodesList = new ArrayList<Map<String,String>>();
		if(id==null || id.equals("")){
			Map<String,String> map = new HashMap<String,String>();
			map.put("id", "0");
			map.put("name", "根目录");
			map.put("isParent","true");
			map.put("checked", "true");
			nodesList.add(map);
		}else{
			List<Resource> resourceList = resourceMapper.queryByParentId(id);
			
			for(Resource e:resourceList){
				String permisId=e.getPermisId();
				String name=e.getPermisName();
				boolean isParent = false;
				boolean checked = false;
				//判断是否是父节点
				if(resourceMapper.isHadSon(permisId).size()>0){
					isParent = true;
				}
				//判断是否为选中状态
				for(RoleResource rr:rrList){
					if(permisId.equals(rr.getResourceid())){
						checked = true;
					}
				}
				Map<String,String> map = new HashMap<String,String>();
				map.put("id", permisId);
				map.put("name",name);
				map.put("isParent",""+isParent);
				map.put("checked", ""+checked);
				nodesList.add(map);
			}
		}
		
		return nodesList;
	}
	
	
	@RequestMapping(value="/getResourceByName/{userName}",method=RequestMethod.GET)
	public List<Resource> getSourceByName(@PathVariable("userName") String userName){
		List<Resource> resourceList = resourceMapper.getResourceByAccount(userName);
		return resourceList;
	}
		
}
